;; package
;; 确保 package 包管理初始化
(require 'package)
(setq package-archives '(("gnu"    . "https://mirrors.tuna.tsinghua.edu.cn/elpa/gnu/")
                         ("nongnu" . "https://mirrors.tuna.tsinghua.edu.cn/elpa/nongnu/")
                         ("melpa"  . "https://mirrors.tuna.tsinghua.edu.cn/elpa/melpa/")))
(package-initialize) ;; You might already have this line

;; 安装 use-package 如果尚未安装
(unless (package-installed-p 'use-package)
  (package-refresh-contents)
  (package-install 'use-package))

(eval-when-compile
  (require 'use-package))
(setq use-package-always-ensure t)

(use-package restart-emacs
  :ensure t)

(use-package simple
  :ensure nil
  :hook (after-init . size-indication-mode)
  :init
  (progn
    (setq column-number-mode t)
    ))

;; 配置 flycheck 进行语法检查
; (use-package flycheck
;   :hook (after-init . global-flycheck-mode))

;; 自动补全
;; (use-package company
;;   :bind (:map company-active-map
;;               ("C-n" . 'company-select-next)
;;               ("C-p" . 'company-select-previous))
;;   :init
;;   (global-company-mode t)
;;   :config
;;   (setq company-minimum-prefix-length 2)
;;   (setq company-idle-delay 0.2)
;;   (setq company-transformers '(company-sort-by-occurrence))
;;   )
(use-package company
  :ensure t
  :init (global-company-mode)
  :bind (:map company-active-map
              ("C-n" . 'company-select-next)
              ("C-p" . 'company-select-previous))
  :config
  (setq company-minimum-prefix-length 2) ; 只需敲 1 个字母就开始进行自动补全
  (setq company-tooltip-align-annotations t)
  (setq company-idle-delay 0.1)
  ;; (setq company-show-numbers t) ;; 给选项编号 (按快捷键 M-1、M-2 等等来进行选择).
  (setq company-selection-wrap-around t)
  (setq company-transformers '(company-sort-by-occurrence))) ; 根据选择的频率进行排序，读者如果不喜欢可以去掉

(use-package company-box
  :ensure t
  :if window-system
  :hook (company-mode . company-box-mode))

(use-package consult
  :ensure t)

(use-package vertico
  :ensure t
  :hook (after-init . vertico-mode))

(use-package orderless
  :ensure t
  :init
  (setq completion-styles '(orderless)))

(use-package corfu
  :ensure t
  :hook (after-init . global-corfu-mode)
  :custom
  (corfu-auto t)
  (corfu-auto-deply 0.1)
  (corfu-min-width 1)
  :init
  (corfu-history-mode)
  (corfu-popupinfo-mode))

(use-package cape
  :ensure t
  :init
  (add-to-list 'completion-at-point-functions #'cape-dabbrev)
  (add-to-list 'completion-at-point-functions #'cape-file)
  (add-to-list 'completion-at-point-functions #'cape-keyword)
  (add-to-list 'completion-at-point-functions #'cape-ispell)
  (add-to-list 'completion-at-point-functions #'cape-dict)
  (add-to-list 'completion-at-point-functions #'cape-symbol)
  (add-to-list 'completion-at-point-functions #'cape-line))

(use-package marginalia
  :init
  (marginalia-mode t))

(use-package ripgrep
  :ensure t
  :after consult
  :bind
  (("C-s" . consult-ripgrep))
  )

(use-package keycast
  :init
  (keycast-mode-line-mode t))

(use-package doom-modeline
  :ensure t
  :init
  (doom-modeline-mode t))

(use-package evil
  :ensure t
  :init
  (setq evil-want-keybinding nil)
  (setq evil-want-C-u-scroll t)
  (setq evil-search-wrap nil)
  (evil-mode 1)
  (evil-select-search-module 'evil-search-module 'evil-search)
  (with-eval-after-load 'evil-maps
    (define-key evil-motion-state-map (kbd "RET") nil))
  )

(cl-loop for (mode state) in '((inferior-emacs-lisp-mode emacs)
				 (nrepl-mode insert)
				 (pylookup-mode emacs)
				 (comint-mode normal)
				 (shell-mode insert)
				 (git-commit-mode insert)
				 (git-rebase-mode emacs)
				 (term-mode emacs)
				 (help-mode emacs)
				 (helm-grep-mode emacs)
				 (grep-mode emacs)
				 (bc-menu-mode emacs)
				 (magit-branch-manager-mode emacs)
				 (rdictcc-buffer-mode emacs)
				 (dired-mode emacs)
				 (wdired-mode normal))
	 do (evil-set-initial-state mode state))

(use-package savehist
  :ensure nil
  :hook (after-init . savehist-mode)
  :custom
  (savehist-file (locate-user-emacs-file ".cache/savehist")) ;; 设置保存文件的位置
  (savehist-additional-variables '(kill-rings shell-command-history)) ;; 额外保存剪切板和shell命令行历史
  (savehist-ignored-variables '(message-history)) ;; 不保存消息历史
  (history-delete-duplicates t) ;; 自动去重
  (history-length 1000) ;; 保存历史数据条目
  )

(use-package recentf
  :ensure nil
  :hook (after-init . recentf-mode)
  :custom
  (recentf-max-menu-item 10) ;; 最多只记录10条历史记录
  (recentf-save-file (locate-user-emacs-file ".cache/recentf")) ;; 定义保存历史记录的临时文件路径
)

;; 记录上次打开位置
(setq save-place-file (locate-user-emacs-file ".cache/places"))
(save-place-mode t)

(provide `init-package)
